#include "StdAfx.h"

TexSelUI::TexSelUI(TextureUI* pTexUI) : m_pTexUI(pTexUI)
{
	m_pTileset = new TilesetUI(this);
	m_pTileset->Hide();
	m_dialog = new Dialog(100, 100, 10, 11, "Textures");
	sUI.AddElement(m_dialog);
	m_dialog->AddText(20, 35, "Enter tileset:", 39);
	m_zoneName = m_dialog->AddEditBox(20, 53, 160, 20);
	m_dialog->AddButton(this, 190, 50, "Load", &TexSelUI::LoadButtonProc, 51, 25, 3);
	m_pZone = m_dialog->AddRadioButton(20, 300, this, &TexSelUI::TabPageChangeProc);
	m_pZone->SetText("Zone");
	m_pZone->Check();
	m_pADT = m_dialog->AddRadioButton(80, 300, this, &TexSelUI::TabPageChangeProc);
	m_pADT->SetText("ADT");
	m_pChunk = m_dialog->AddRadioButton(140, 300, this, &TexSelUI::TabPageChangeProc);
	m_pChunk->SetText("Chunk");
	m_pLast = m_dialog->AddRadioButton(210, 300, this, &TexSelUI::TabPageChangeProc);
	m_pLast->SetText("Last 50", 40);
	m_dialog->AddText(20, 280, "Searchmode:", 41);
	m_dialog->AddButton(this, 250, 50, "Add", &TexSelUI::AddFavButtonProc, 51, 25, 42);
	UIButton<TexSelUI>* pButton = m_dialog->AddButton(this, 150, 15, "", &TexSelUI::ListButtonProc, 35, 15);
	pButton->SetTextures("Interface\\Buttons\\UI-PlusButton-Up.blp", "Interface\\Buttons\\UI-PlusButton-Down.blp");
	Vertex2F c[4];
	c[0].x = c[0].y = c[1].y = c[3].x = 0;
	c[1].x = c[2].x = c[2].y = c[3].y = 1;
	pButton->SetCoordinates(c);
}

void TexSelUI::SetZone(std::string szZone)
{
	m_quads.clear();
	m_dialog->ResetQuads();
	CMpqFile tileset(szZone);
	if(!tileset.IsOpened())
	{
		sHud.SetText("Tileset not found!", 5000, 0xFF7F00, 43);
		return;
	}
	char* pBuffer = new char[tileset.GetSize()];
	tileset.Read(0, tileset.GetSize(), pBuffer);
	std::string files = pBuffer;
	delete [] pBuffer;
	std::string::size_type pos;
	std::vector<std::string> vTextures;
	while((pos = files.find('\n')) != std::string::npos)
	{
		std::string tmp = files.substr(0, pos - 2);
		if(tmp.find("_s.blp") == -1 && tmp.find("alpha.blp") == -1)
			vTextures.push_back(tmp);
		files = files.substr(pos + 1, files.length());
	}

	bool stop = false;
	
	for(ui32 i = 0; i < 5; ++i)
	{
		for(ui32 j = 0; j < 7; ++j)
		{
			ui32 num = i * 7 + j;
			if(num >= vTextures.size())
			{
				stop = true;
				break;
			}
			TextureQuad<TexSelUI>* q = m_dialog->AddTexQuad(this, &TexSelUI::TextureQuadClick, vTextures[num], Vertex2F(j * (32 + 5) + 20, i * (32 + 5) + 90));
			q->visible = true;
			m_quads.push_back(q);
		}
		if(stop)
			break;
	}
}

void TexSelUI::LoadButtonProc(UIButton<TexSelUI>* pButton)
{
	m_quads.clear();
	m_dialog->ResetQuads();
	CMpqFile tileset(m_zoneName->GetText());
	if(!tileset.IsOpened())
	{
		sHud.SetText("Tileset not found!", 5000, 0xFF7F00, 43);
		return;
	}
	char* pBuffer = new char[tileset.GetSize()];
	tileset.Read(0, tileset.GetSize(), pBuffer);
	std::string files = pBuffer;
	delete [] pBuffer;
	std::string::size_type pos;
	std::vector<std::string> vTextures;
	while((pos = files.find('\n')) != std::string::npos)
	{
		std::string tmp = files.substr(0, pos - 2);
		vTextures.push_back(tmp);
		files = files.substr(pos + 1, files.length());
	}

	bool stop = false;
	
	for(ui32 i = 0; i < 5; ++i)
	{
		for(ui32 j = 0; j < 7; ++j)
		{
			ui32 num = i * 7 + j;
			if(num >= vTextures.size())
			{
				stop = true;
				break;
			}
			TextureQuad<TexSelUI>* q = m_dialog->AddTexQuad(this, &TexSelUI::TextureQuadClick, vTextures[num], Vertex2F(j * (32 + 5) + 20, i * (32 + 5) + 90));
			q->visible = true;
			m_quads.push_back(q);
		}
		if(stop)
			break;
	}
}

void TexSelUI::TextureQuadClick(TextureQuad<TexSelUI>* pQuad)
{
	pQuad->border = !pQuad->border;
}

void TexSelUI::AddFavButtonProc(UIButton<TexSelUI>*)
{
	for(ui32 i = 0; i < m_quads.size(); ++i)
	{
		if(m_quads[i]->border)
		{
			bool add = true;
			for(ui32 j = 0; j < m_lastZones.size() && j < 50; ++j)
				if(m_lastZones[j] == m_quads[i]->szTexture)
					add = false;
			if(add)
				m_lastZones.push_front(m_quads[i]->szTexture);
			m_pTexUI->AddFavourite(m_quads[i]->szTexture);
			m_quads[i]->border = false;
		}
	}
}

void TexSelUI::ListButtonProc(UIButton<TexSelUI>*)
{
	m_pTileset->Show();
	sDialog.BringToFront(*m_pTileset);
}

void TexSelUI::TabPageChangeProc(RadioButton<TexSelUI>* pButton, bool checked)
{
	if(!checked)
	{
		pButton->Check();
		return;
	}
	if(pButton == m_pZone)
	{
		m_quads.clear();
		m_dialog->ResetQuads();
		m_pADT->Uncheck();
		m_pChunk->Uncheck();
		m_pLast->Uncheck();
	}
	else if(pButton == m_pADT)
	{
		m_quads.clear();
		m_dialog->ResetQuads();
		m_pTileset->Uncheck();
		Vertex3F cp = sCamera.GetCamPosition();
		ui32 indexX = (ui32)floor(cp.x / TILESIZE);
		ui32 indexY = (ui32)floor(cp.z / TILESIZE);
		ADTModel* model = sD3D.GetADT(indexX, indexY);
		m_lastModel = model;
		if(model)
		{
			bool stop = false;
	
			for(ui32 i = 0; i < 5; ++i)
			{
				for(ui32 j = 0; j < 7; ++j)
				{
					ui32 num = i * 7 + j;
					if(num >= model->GetTextures().size())
					{
						stop = true;
						break;
					}
					TextureQuad<TexSelUI>* q = m_dialog->AddTexQuad(this, &TexSelUI::TextureQuadClick, model->GetTextures()[num], Vertex2F(j * (32 + 5) + 20, i * (32 + 5) + 90));
					q->visible = true;
					m_quads.push_back(q);
				}
				if(stop)
					break;
			}
		}
		m_pZone->Uncheck();
		m_pChunk->Uncheck();
		m_pLast->Uncheck();
	}
	else if(pButton == m_pChunk)
	{
		m_quads.clear();
		m_dialog->ResetQuads();
		m_pTileset->Uncheck();
		m_pZone->Uncheck();
		m_pADT->Uncheck();
		m_pLast->Uncheck();
		Vertex3F pos = sCamera.GetCamPosition();
		ui32 indexX = (ui32)floor(pos.x / TILESIZE);
		ui32 indexY = (ui32)floor(pos.z / TILESIZE);
		ADTModel* mod = sD3D.GetADT(indexX, indexY);
		if(!mod)
		{
			m_lastChunk = 0;
			return;
		}
		float rx = pos.x - (indexX * TILESIZE);
		float ry = pos.z - (indexY * TILESIZE);
		indexX = (ui32)floor(rx / CHUNKSIZE);
		indexY = (ui32)floor(ry / CHUNKSIZE);
		ADTChunk* c = mod->GetChunk(indexX, indexY);
		m_lastChunk = c;
		if(!c)
			return;
		bool stop = false;
	
		for(ui32 i = 0; i < 5; ++i)
		{
			for(ui32 j = 0; j < 7; ++j)
			{
				ui32 num = i * 7 + j;
				if(num >= c->GetTextures().size())
				{
					stop = true;
					break;
				}
				TextureQuad<TexSelUI>* q = m_dialog->AddTexQuad(this, &TexSelUI::TextureQuadClick, c->GetTextures()[num], Vertex2F(j * (32 + 5) + 20, i * (32 + 5) + 90));
				q->visible = true;
				m_quads.push_back(q);
			}
			if(stop)
				break;
		}
	}
	else if(pButton == m_pLast)
	{
		m_pZone->Uncheck();
		m_pADT->Uncheck();
		m_pChunk->Uncheck();

		m_quads.clear();
		m_dialog->ResetQuads();
		m_pTileset->Uncheck();

		bool stop = false;
	
		for(ui32 i = 0; i < 5; ++i)
		{
			for(ui32 j = 0; j < 7; ++j)
			{
				ui32 num = i * 7 + j;
				if(num >= m_lastZones.size())
				{
					stop = true;
					break;
				}
				TextureQuad<TexSelUI>* q = m_dialog->AddTexQuad(this, &TexSelUI::TextureQuadClick, m_lastZones[num], Vertex2F(j * (32 + 5) + 20, i * (32 + 5) + 90));
				q->visible = true;
				m_quads.push_back(q);
			}
			if(stop)
				break;
		}
	}
}

void TexSelUI::Update()
{
	if(!m_dialog->m_visible)
		m_pTileset->Hide();

	if(m_pChunk->GetChecked())
	{
		Vertex3F pos = sCamera.GetCamPosition();
		ui32 indexX = (ui32)floor(pos.x / TILESIZE);
		ui32 indexY = (ui32)floor(pos.z / TILESIZE);
		ADTModel* mod = sD3D.GetADT(indexX, indexY);
		if(!mod)
			return;
		float rx = pos.x - (indexX * TILESIZE);
		float ry = pos.z - (indexY * TILESIZE);
		indexX = (ui32)floor(rx / CHUNKSIZE);
		indexY = (ui32)floor(ry / CHUNKSIZE);
		ADTChunk* c = mod->GetChunk(indexX, indexY);
		if(c == m_lastChunk)
			return;

		m_quads.clear();
		m_dialog->ResetQuads();
		m_pTileset->Uncheck();

		m_lastChunk = c;
		if(!c)
			return;

		bool stop = false;
	
		for(ui32 i = 0; i < 5; ++i)
		{
			for(ui32 j = 0; j < 7; ++j)
			{
				ui32 num = i * 7 + j;
				if(num >= c->GetTextures().size())
				{
					stop = true;
					break;
				}
				TextureQuad<TexSelUI>* q = m_dialog->AddTexQuad(this, &TexSelUI::TextureQuadClick, c->GetTextures()[num], Vertex2F(j * (32 + 5) + 20, i * (32 + 5) + 90));
				q->visible = true;
				m_quads.push_back(q);
			}
			if(stop)
				break;
		}
	}
	if(m_pADT->GetChecked())
	{
		Vertex3F cp = sCamera.GetCamPosition();
		ui32 indexX = (ui32)floor(cp.x / TILESIZE);
		ui32 indexY = (ui32)floor(cp.z / TILESIZE);
		ADTModel* model = sD3D.GetADT(indexX, indexY);
		if(model == m_lastModel)
			return;

		m_lastModel = model;

		m_quads.clear();
		m_dialog->ResetQuads();
		m_pTileset->Uncheck();

		if(model)
		{
			bool stop = false;
	
			for(ui32 i = 0; i < 5; ++i)
			{
				for(ui32 j = 0; j < 7; ++j)
				{
					ui32 num = i * 7 + j;
					if(num >= model->GetTextures().size())
					{
						stop = true;
						break;
					}
					TextureQuad<TexSelUI>* q = m_dialog->AddTexQuad(this, &TexSelUI::TextureQuadClick, model->GetTextures()[num], Vertex2F(j * (32 + 5) + 20, i * (32 + 5) + 90));
					q->visible = true;
					m_quads.push_back(q);
				}
				if(stop)
					break;
			}
		}
	}
}